from interface import user_interface,bank_interface,common_interface,shopping_interface
from lib import common
from core import admin_manager,shopping_main
from conf import setting
import os

user_info={'username':None,'role':None,'count':0,'balance':None}


#1.注册功能
def register():
    print('注册页面')

    while True:
        name=input('请输入您的用户名:').strip()
        pwd=input('请输入您的密码:').strip()
        conf_pwd=input('请再次确认您的密码:').strip()


        if pwd==conf_pwd:
            flag,msg=user_interface.register_interface(name,pwd)
            if flag:
                print(msg)
                break
            else:
                print(msg)
        else:
            print('两次密码不一致，请核对后重新输入.')

#2.登陆功能
def login():
    print('登陆页面')
    while True:
        if user_info['username']:
            print('用户%s 已登陆，请勿重复登陆!'%user_info['username'])
            break


        name=input('请输入用户名:').strip()
        pwd=input('请输入密码:').strip()

        flag,msg=user_interface.login_interface(name,pwd,user_info['count'])
        if flag:
            print('%s 登陆成功!'%name)
            user_info['username']=name
            user_info['role']=msg['role']
            user_info['balance']=msg['balance']
            break
        else:
            user_info['count']+=1
            print(msg)



#3.查看用户的账号及余额信息
@common.login_auth
def check_account_balance():
    print('查看账户及余额页面')
    flag,msg=bank_interface.check_account_balance_interface(user_info['username'])
    print('-'*50)
    if flag:

        print('''
        账号:%s,
        角色:%s,
        信用额度:%s,
        账户余额:%s
        '''%(msg['name'],msg['role'],msg['credit'],msg['balance']))
    else:
        print(msg)

    print('-'*50)



#4.转账功能
@common.login_auth
def transfer():
    print('转账页面')
    while True:
        to_user=input('请输入对方的账号:').strip()
        to_money=input('请输入转账的金额:').strip()
        if to_money.isdigit():
            to_money=int(to_money)
            flag,msg=bank_interface.transfer_interface(to_user,to_money,user_info['username'])
            if flag:
                print(msg)
                break
            else:
                print(msg)
        else:
            print('金额请输入数字...')


#5.取款功能
@common.login_auth
def withdraw():
    print('取款页面')
    while True:
        with_money=input('请输入取款的金额<quit 退出>:').strip()
        if with_money.isdigit():
            with_money=int(with_money)
            flag,msg=bank_interface.withdraw_interface(with_money,user_info['username'])
            if flag:
                print(msg)
                break
            else:
                print(msg)
        else:
            if with_money=='quit':
                break
            print('取款金额请输入整数...')


#6.还款功能,当账户余额 < 信用额度的时候可还款！
@common.login_auth
def repay():
    print('还款页面')
    while True:
        repay_money=input('请输入还款的金额 <quit 退出>:').strip()
        if repay_money.isdigit():
            repay_money=int(repay_money)
            flag,msg=bank_interface.repay_interface(repay_money,user_info['username'])
            if flag:
                print(msg)
                break
            else:
                print(msg)
        else:
            if repay_money=='quit':
                break
            print('还款金额请输入整数...')




#7.查看流水
@common.login_auth
def check_records():
    print('查看用户')
    status=True
    while status:
        flag,log_list=common_interface.check_records_interface(user_info['username'],os.path.join(setting.LOG_PATH,user_info['role']))
        if flag:
            while status:
                print("="*100)
                for n,line in enumerate(log_list):
                    print('%s : %s'%(n,line))
                choice=input('请选择要查看的日志<q 退出>:').strip()
                if choice=='q':status=False
                if not choice.isdigit():continue
                choice=int(choice)
                user_path=os.path.join(setting.LOG_PATH,user_info['role'])
                log_path=os.path.join(user_path,log_list[choice])
                with open(log_path,'r',encoding='utf-8') as f:
                    for line in f:
                        print(line)

        else:
            print(log_list)
            break



#8.购物
business_dic={
        '0':shopping_main.check_goods,
        '1':shopping_main.add_goods,
        '2':shopping_main.remove_goods,
        '3':shopping_main.update_goods
    }

def business_mod():


    while True:
        print('=================商家功能======================')
        print('''
            0.查看商品列表
            1.上架商品
            2.下降商品
            3.修改商品价格和库存
            ''')
        choice=input('请输入商家功能 <quit 退出>').strip()
        if choice.isdigit():
            business_dic[choice]()
        elif choice=='quit':
            break
        else:
            print('请输入对应的数字')
            break



def consumer_mod():
    print('==============用户入口=========================')
    while True:
        goods_list = []
        choice_list = []
        consumer_sum = 0
        flag, msg = shopping_interface.check_goods_interface(setting.GOODS_PATH)
        if flag:
            for line in msg:
                goods_list.append(line.replace('.json', ''))
            for n, good in enumerate(goods_list):
                choice_list.append(n)
                print('%s.%s' % (n, good))
            print('-' * 100)
            choice=input('请选择要购买的商品 <quit 退出>:').strip()
            if choice.isdigit():
                choice = int(choice)
                if choice in choice_list:

                    flag,msg=shopping_interface.buy_goods_interface(user_info['username'],goods_list[choice])
                    if flag:
                        print(msg)
                    else:
                        print(msg)
                else:
                    print('请是输入商品对应的编号..')
            else:
                if choice=='quit':
                    flag,msg=shopping_interface.consumer_list(user_info['username'])
                    if flag:
                        print(msg)
                        break
                    else:
                        print(msg)
                        break
                else:
                    print('请输入对应的数字...')
        else:#没有商品的情况
            print(msg)
            break





@common.login_auth
def shopping():
    shopping_dic={'1':consumer_mod,'2':business_mod}
    print('Welcome %s Shopping Go'%user_info['username'])
    while True:
        print('''
        请选择:
        1.用户<普通用户>
        
        2.商家<管理员>
        ''')
        choice=input('请选择进入<quit 退出>:').strip()
        if choice.isdigit():
            if choice=='1' and user_info['role']=='user':
                shopping_dic['1']()
            elif choice=='2' and user_info['role']=='admin':
                shopping_dic['2']()
            else:
                print('请核对后权限后重新选择!')

        elif choice=='quit':
            break
        else:
            print('请输入对应的数字')



#9.查看购物车
@common.login_auth
def check_shoppingcar():
    print('============查看购物车商品列表============')
    while True:
        flag,msg=shopping_interface.check_shoppingcar(user_info['username'])
        if flag:
            for good,count in msg.items():
                print('%s ： %s <购买数量> '%(good,count))
            break
        else:
            print(msg)
            break




manager_dic={
    '1':admin_manager.thawing_account,
    '2':admin_manager.freeze_account,
    '3':admin_manager.increase_credit,
    '4':admin_manager.decrease_credit,
    '5':admin_manager.reset_password,
    '6':admin_manager.add_user

}

#10.管理员入口
@common.login_auth
def admin_manager():
    if user_info['role']=='admin':
        while True:
            print('''
                    1.解冻账号
                    2.冻结账号
                    3.增加额度
                    4.降低额度
                    5.重置密码
                    6.添加账户
                    ''')
            choice = input('请选择<quit 退出>:').strip()
            if choice in manager_dic:
                manager_dic[choice]()
            else:
                if choice == 'quit':
                    print('bye...')
                    break
                else:
                    print('请输入列表中的编号..')
                    continue
    else:
        print('您不是管理员,请选择其他功能...')




main_dic={
    '1':register,
    '2':login,
    '3':check_account_balance,
    '4':transfer,
    '5': withdraw,
    '6':repay,
    '7':check_records,
    '8':shopping,
    '9':check_shoppingcar,
    '10':admin_manager
}




def run():
    while True:
        print('''
        1.注册
        2.登陆
        3.查看账户及余额
        4.转账
        5.取款
        6.还款
        7.查看流水
        8.购物
        9.查看购买商品
        10.管理员入口
        ''')
        choice=input('请选择<quit 退出>:').strip()
        if choice  in main_dic:
            main_dic[choice]()
        else:
            if choice=='quit':
                print('bye...')
                break
            else:
                print('请输入列表中的编号..')
                continue